package com.smz.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smz.entity.TLaser;
import com.smz.entity.TPrinter;
import com.smz.entity.TProductOrder;
import com.smz.entity.TProductionLine;
import com.smz.service.TLaserService;
import com.smz.service.TPrinterService;
import com.smz.service.TProductOrderService;
import com.smz.service.TProductionLineService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 生产模块
 * @author Kan
 * @Time 2021-11-16-20:03
 */
@Api
@RestController
@RequestMapping("/produce")
public class ProduceController {

    @Autowired
    TProductionLineService lineService;

    @Autowired
    TProductionLineService LineService;

    @Autowired
    TPrinterService tPrinterService;

    @Autowired
    TProductOrderService tProductOrderService;

    @Autowired
    TLaserService tLaserService;

    @ApiOperation("查询所有生产线")
    @GetMapping("/lines")
    public ResponseEntity findAllLines(){
        List<TProductionLine> lines = lineService.findAllProductionLine();
        return ResponseEntity.ok(lines);
    }

    @ApiOperation("新增生产线")
    @PostMapping("/line")
    public ResponseEntity insertLines(@RequestBody TProductionLine pro){
        String s = lineService.addProduction(pro);
        return ResponseEntity.ok(s);
    }

    @ApiOperation("更新生产线")
    @PutMapping("/line")
    public ResponseEntity updateLines(@RequestBody TProductionLine pro){
        String s = lineService.modifyProduction(pro);
        return ResponseEntity.ok(s);
    }

    @ApiOperation("删除生产线")
    @DeleteMapping("/line")
    public ResponseEntity removeLine(Integer id){
        String s = lineService.removeLine(id);
        return ResponseEntity.ok(s);
    }

    @ApiOperation("查询所有的负责人")
    @GetMapping("/operators")
    public ResponseEntity findAllOrdersByPage(){
        List<String> list = lineService.selectAllOperators();
        return  ResponseEntity.ok(list);
    }

    @ApiOperation("查询所有带生产订单数量")
    @GetMapping("/readyOrder")
    public ResponseEntity findReadyNumber(){
        Long number = tProductOrderService.selectReadyProduct();
        return ResponseEntity.ok(number);
    }

    @ApiOperation("查询分页所有订单")
    @GetMapping("/orders")
    public ResponseEntity findAllOrders(@RequestParam(required = false ,defaultValue = "1")
        Integer current,@RequestParam(required = false ,defaultValue = "2")Long pageSize){
        Page order = tProductOrderService.findAllOrderByPage(current, pageSize);
        order.getRecords().forEach(System.out::println);
        return ResponseEntity.ok(order);
    }

    @ApiOperation("查询指定订单")
    @GetMapping("/order")
    public ResponseEntity findOrder(@RequestParam(required = false) Integer orderId,
    @RequestParam(required = false)Long orderType,
    @RequestParam(required = false ,defaultValue = "1")Integer current,
    @RequestParam(required = false ,defaultValue = "2")Long pageSize){
        if (!"null".equals(String.valueOf(orderId))){
            //如果指定了订单id，就查询指定订单
            List<TProductOrder> orderDesign = tProductOrderService.findOrderDesign(orderId);
            return ResponseEntity.ok(orderDesign);
        }else if (orderType!=null){
            Page<TProductOrder> doneOrReady =
                    tProductOrderService.findDoneProductByPage(current, pageSize, orderType);
            return ResponseEntity.ok(doneOrReady);
        }else {
            Page<TProductOrder> allOrderByPage = tProductOrderService.findAllOrderByPage(current, pageSize);
            return ResponseEntity.ok(allOrderByPage);
        }
    }

    @ApiOperation("加入到激光码生成列表")
    @PutMapping("/toLaser")
    public ResponseEntity toLaser(@RequestBody TProductOrder tProductOrder){
        String s = tProductOrderService.updateOrder(tProductOrder);
        return  ResponseEntity.ok(s);
    }

    @ApiOperation("查询所有的打印机")
    @GetMapping("/printers")
    public ResponseEntity printers(){
        List<TPrinter> list = tPrinterService.findAllPrinters();
        System.out.println(list);
        return ResponseEntity.ok(list);
    }

    @ApiOperation("查询指定打印机")
    @GetMapping("/printer")
    public ResponseEntity getPrinter(String pNum){
        List<TPrinter> list = tPrinterService.findPrinters(pNum);
        System.out.println(list);
        return ResponseEntity.ok(list);
    }

    @ApiOperation("新增打印机")
    @PostMapping("/printer")
    public ResponseEntity inserPrinter(@RequestBody TPrinter tPrinter){
        String s = tPrinterService.addPrinter(tPrinter);
        return ResponseEntity.ok(s);
    }

    @ApiOperation("查看打印机是否已经存在")
    @GetMapping("/pNum")
    public ResponseEntity testPrinter(String pNum){
        String s = tPrinterService.testPrintNum(pNum);
        return ResponseEntity.ok(s);
    }

    @ApiOperation("更新打印机")
    @PutMapping("/printer")
    public ResponseEntity updatePrinter(@RequestBody TPrinter tPrinter){
        String s = tPrinterService.updatePrinter(tPrinter);
        return ResponseEntity.ok(s);
    }

    @ApiOperation("删除打印机")
    @DeleteMapping("/printer")
    public ResponseEntity deletePrinter(@RequestBody TPrinter data){
        String s = tPrinterService.removePrinter(data);
        return ResponseEntity.ok(s);
    }

    @ApiOperation("删除多个打印机")
    @DeleteMapping("/printers")
    public ResponseEntity deletePrinters(String[] pNums){
        String rs = tPrinterService.removePrinterBranch(pNums);
        return ResponseEntity.ok(rs);
    }

    @ApiOperation("新增激光码")
    @PostMapping("/laser")
    public ResponseEntity insertLaser(String[] orderIds ){
        List<TLaser> tLasers = tLaserService.addToLaser(orderIds);
        return ResponseEntity.ok(tLasers);
    }

    @ApiOperation("批量删除激光码")
    @DeleteMapping("/laser")
    public ResponseEntity removeLaser(Integer[] delId ){
        String s = tLaserService.removeLaser(delId);
        return ResponseEntity.ok(s);
    }

    @ApiOperation("分页查询打印订单")
    @GetMapping("/printOrders")
    public ResponseEntity getPrintOrders(
            @RequestParam(required = false) Long orderId,
            @RequestParam(required = false,defaultValue = "2")Long orderStatus,
            @RequestParam(required = false ,defaultValue = "1")Integer current,
            @RequestParam(required = false ,defaultValue = "2")Long pageSize){
        //查询打印订单
        Page<TLaser> laserOrders = tLaserService.findLaserOrders(current, pageSize, orderId, orderStatus);
        System.out.println(laserOrders.getCurrent());
        System.out.println(laserOrders.getPages());
        return ResponseEntity.ok(laserOrders);
    }

    @ApiOperation("派发打印")
    @PutMapping("/toPrint")
    public ResponseEntity toPrint(String printer,Integer[] laserIdArr,Integer[] orderIdArr){
        System.out.println(printer);
        System.out.println(Arrays.toString(laserIdArr));
        System.out.println(Arrays.toString(orderIdArr));
        String s = tLaserService.toPrint(printer, laserIdArr, orderIdArr);
        return ResponseEntity.ok(s);
    }

    @ApiOperation("自动派发打印")
    @PutMapping("/autoToPrint")
    public ResponseEntity toAutoPrint(Integer[] orderIdArr){
        System.out.println(Arrays.toString(orderIdArr));
        String s = tLaserService.autoToPrint(orderIdArr);
        return ResponseEntity.ok(s);
    }

    @ApiOperation("删除派发打印")
    @DeleteMapping("/removeToPrint/{orderIds}")
    public ResponseEntity removePrints(@PathVariable("orderIds") Integer[] orderIdArr){
        String s = tLaserService.removeToPrint(orderIdArr);
        return ResponseEntity.ok(s);
    }

}
